package com.example.bbs.util;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.Base64;
import android.util.Log;

import androidx.core.content.FileProvider;

import com.example.bbs.base.App;
import com.example.bbs.base.Data;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class ImageUtil {

    static final String LOG_TAG = ImageUtil.class.getSimpleName();

    private ImageUtil() throws InstantiationException {
        throw new InstantiationException("This class is not for instantiation");
    }

    public static int KEEP_ASPECT_RATIO = 0;

    public static Bitmap zoom(Bitmap inBitmap, float x, float y) {
        Matrix scaleMatrix = new Matrix();
        scaleMatrix.postScale(x, y);
        return Bitmap.createBitmap(inBitmap, 0, 0, inBitmap.getWidth(),
                inBitmap.getHeight(), scaleMatrix, true);
    }

    public static Bitmap resize(Bitmap inBitmap, int widthInPx, int heightInPx) {
        float x, y;
        if (widthInPx == KEEP_ASPECT_RATIO && heightInPx == KEEP_ASPECT_RATIO || widthInPx < 0 || heightInPx < 0) {
            x = y = 1;
        } else if (widthInPx == KEEP_ASPECT_RATIO) {
            x = y = (float) heightInPx / inBitmap.getHeight();
        } else if (heightInPx == KEEP_ASPECT_RATIO) {
            x = y = (float) widthInPx / inBitmap.getWidth();
        } else {
            x = (float) widthInPx / inBitmap.getWidth();
            y = (float) heightInPx / inBitmap.getHeight();
        }
        return zoom(inBitmap, x, y);
    }

    @SuppressLint("ObsoleteSdkInt")
    public static Uri saveBitmapToCache(Bitmap bitmap) {
        String name = new SimpleDateFormat("yyyy-MM-dd#HH-mm-ss", Locale.CHINA)
                .format(Calendar.getInstance().getTime());
        String extension = "jpg";
        File imageFile = new File(Data.context.getExternalCacheDir(), name + '.' + extension);
        if (!imageFile.exists()) {
            try {
                if (!imageFile.createNewFile()) {
                    Log.d(LOG_TAG, "create file error");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bitmap != null) {
            try {
                FileOutputStream saveImgOut = new FileOutputStream(imageFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, saveImgOut);
                saveImgOut.flush();
                saveImgOut.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return FileProvider.getUriForFile(Data.context, App.AUTHORITY, imageFile);
        } else {
            return Uri.fromFile(imageFile);
        }
    }
}
